package com.python.project2.config;

import com.python.project2.filter.CORSFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;

import javax.servlet.Filter;
import java.util.Arrays;

/**
 * @Description 将filter注册到IOC容器中
 * @Date 2023/3/31
 * @Author lixian
 **/
@Configuration
public class FilterRegisterConfig {

	@Bean(name = "CORSFilter")
	public FilterRegistrationBean CORSFilterRegistration() {
		FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
		registrationBean.setFilter(new CORSFilter());
		registrationBean.setUrlPatterns(Arrays.asList("/*"));
		registrationBean.setOrder(1);
		return registrationBean;
	}

	@Bean(name = "charsetFilter")
	public FilterRegistrationBean charsetFilterRegistration() {
		FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
		CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
		characterEncodingFilter.setEncoding("UTF-8");
		characterEncodingFilter.setForceEncoding(true);
		registrationBean.setFilter(characterEncodingFilter);
		registrationBean.setUrlPatterns(Arrays.asList("/*"));
		registrationBean.setOrder(2);
		return registrationBean;
	}
}
